<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="搜索条件" prop="type1">
        <el-select v-model="queryParams.type1" placeholder="用户ID/账号" clearable size="small"
          @keyup.enter.native="handleQuery" @change="searchType">
          <el-option v-for="item in searchOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchCon" placeholder="请输入搜索内容" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--      <el-form-item label="充值单号" prop="paymentSn">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.paymentSn"-->
      <!--          placeholder="请输入充值单号"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="下单日期">
        <el-date-picker v-model="dateRange" type="daterange" align="right" value-format="yyyy-MM-dd" size="small"
          style="width: 300px" range-separator="-" start-placeholder="起始时间" end-placeholder="结束时间"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="充值等级" align="center" prop="levelId">
        <template slot-scope="scope">
          <span v-if="scope.row.levelId == 1">普通会员</span>
          <span v-if="scope.row.levelId == 2">金卡会员 </span>
          <span v-if="scope.row.levelId == 3">钻石会员 </span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="orderAmt" />
      <el-table-column label="实付金额" align="center" prop="payAmt" />
      <!--      <el-table-column label="充值单号" align="center" prop="paymentSn" />-->
      <!--      <el-table-column label="购买等级" align="center" prop="levelName" />-->
      <!--      <el-table-column label="支付金额" align="center" prop="orderAmount" />-->
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <span v-if="scope.row.payType == 1">微信</span>
          <span v-if="scope.row.payType == 2">支付宝支付</span>
          <span v-if="scope.row.payType == 3">余额支付</span>
          <span v-if="scope.row.payType == 4">快捷支付</span>
          <!--          <span v-if="scope.row.payType == 6">线下转账</span>-->
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" />
      <el-table-column label="订单状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.payStatus == 0" type="warning">待付款</el-tag>
          <el-tag v-if="scope.row.payStatus == 1" type="success">已付款</el-tag>
          <el-tag v-if="scope.row.payStatus == 2" type="danger">已失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="account" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-circle-check" @click="audit(scope.row, 1)"
            v-if="scope.row.payStatus == 0 && scope.row.payType == 6">审核通过
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-circle-close" @click="audit(scope.row, 2)"
            v-if="scope.row.payStatus == 0 && scope.row.payType == 6">审核拒绝
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="查看详情" :visible.sync="open" append-to-body @close="cancel">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="用户ID" prop="userId">
          {{ form.userId }}
        </el-form-item>
        <el-form-item label="用户账号" prop="account">
          {{ form.account }}
        </el-form-item>
        <el-form-item label="单号" prop="paymentSn">
          {{ form.paymentSn }}
        </el-form-item>
        <el-form-item label="购买等级" prop="levelId">
          <span v-if="form.levelId == 1">普通会员</span>
          <span v-if="form.levelId == 2">金卡会员 </span>
          <span v-if="form.levelId == 3">钻石会员 </span>
        </el-form-item>
        <el-form-item label="订单状态" prop="payStatus">
          <el-tag v-if="form.payStatus == 0" type="warning">待付款/待审核</el-tag>
          <el-tag v-if="form.payStatus == 1" type="success">已付款/审核通过</el-tag>
          <el-tag v-if="form.orderStatus == 2" type="danger">已失效/审核拒绝</el-tag>
        </el-form-item>
        <el-form-item label="创建时间" prop="orderTime">
          {{ form.orderTime }}
        </el-form-item>
        <el-form-item label="付款时间" prop="payTime" v-show="form.payType != 6">
          {{ form.payTime }}
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime" v-show="form.payType == 6">
          {{ form.auditTime }}
        </el-form-item>
        <el-form-item label="付款方式" prop="payType" v-for="(item, index) in payTypeOption" :key="index"
          v-show="form.payType == item.dictValue">
          <el-tag type="success">{{ item.dictLabel }}</el-tag>
        </el-form-item>
        <el-form-item label="转账截图" v-show="form.payType == 6">
          <el-image :src="requestUrl + form.images" style="width: 140px; height: 140px"
            :preview-src-list="previewImgList" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { taskLevelPaymentListPage, audit } from "@/api/admin/userTask";

export default {
  name: "TaskManage",
  data () {
    return {
      pickerOptions: {
        disabledDate (time) {
          //time跟页面没有关系   相当于日期数组
          return time.getTime() > Date.now(); //设置选择今天以及今天之后的日期
        },
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务详情表格数据
      taskList: [],
      paymentAmountNum: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 1,
      },
      dateRange: [],
      searchOptions: [
        {
          value: "1",
          label: "用户ID",
        },
        {
          value: "2",
          label: "用户账号",
        },
      ],
      searchCon: null,
      activeName: "pending",
      form: {},
      payTypeOption: {},
      requestUrl: this.$img.requestUrl,
      previewImgList: [],
    };
  },
  created () {
    this.getList();
    this.getDicts("pay_type").then((response) => {
      this.payTypeOption = response.data;
    });
  },
  methods: {
    /** 查询任务详情列表 */
    getList () {
      this.loading = true;
      taskLevelPaymentListPage(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      if (this.inputKey === 1) {
        this.queryParams.userId = this.searchCon;
        delete this.queryParams.account;
      } else if (this.inputKey === 2) {
        this.queryParams.account = this.searchCon;
        delete this.queryParams.userId;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    searchType (e) {
      this.inputKey = Number(e);
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.searchCon = "";
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleClick (tab, event) {
      let name = tab.name;
      if (name === "pending") {
        this.queryParams.type = 1;
      } else {
        this.queryParams.type = 2;
      }
      this.getList();
    },
    audit (row, payStatus) {
      this.$confirm("是否确定审核?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return audit({
            paymentId: row.paymentId,
            payStatus: payStatus,
          });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功");
        })
        .catch(() => {
          const that = this;
          that.getList();
        });
    },
    // 查看
    handleView (row) {
      this.form = { ...row };
      this.open = true;
      this.previewImgList = [];
      this.previewImgList.push(this.requestUrl + row.images);
    },
    cancel () {
      this.reset();
      this.open = false;
    },
    // 表单重置
    reset () {
      this.form = {};
      this.resetForm("form");
    },
  },
};
</script>
